//
//  VOCTestListController.m
//  iVoc
//
//  Created by Wallace Wu on 09-4-7.
//  Copyright 2009 Sonic Solutions. All rights reserved.
//

#import "VOCWordTest.h"
#import "VOCTestListController.h"
#import "VOCWordTestViewController.h"

#define SECTION_PAUSED_TEST         2

VOCWORDLanguage Lang[VOCWORDLanguageCount] = {VOC_WORD_LANG_UNKNOWN};

@implementation VOCTestListController

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
{
    self = [super initWithNibName:nibName bundle:nibBundle];
    if (self) {
        
        self.title = NSLocalizedString(@"TEST",nil);
        self.tabBarItem.image = [UIImage imageNamed:@"test.png"];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.title = NSLocalizedString(@"TEST",nil);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [(UITableView*)self.view reloadData];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


-(void)beginToTestView
{
    //first let user choose the language, then let it go
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"CHOOSE_LANGUAGE",nil) delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    VOCWORDLanguage lang = [selectedList explanationLanguages];
    NSUInteger index = 0;
    if(lang & VOC_WORD_LANG_ENGLISH)
    {
        //add english button
        [sheet addButtonWithTitle:NSLocalizedString(@"LANG_ENGLISH",nil)];
        Lang[index] = VOC_WORD_LANG_ENGLISH;
        index++;
    }
    else if( lang & VOC_WORD_LANG_SIMPCHI )
    {
        //add simplified chinese button
        [sheet addButtonWithTitle:NSLocalizedString(@"LANG_SIMPCHI",nil)];
        Lang[index] = VOC_WORD_LANG_SIMPCHI;
        index++;
    }
    [sheet addButtonWithTitle:NSLocalizedString(@"CANCEL",nil)];
    sheet.cancelButtonIndex = sheet.numberOfButtons-1;
    [sheet showInView:self.tabBarController.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex != actionSheet.cancelButtonIndex)
    {
        VOCWORDLanguage selectedLang = Lang[buttonIndex];
        VOCWordTestViewController *testViewController = [[VOCWordTestViewController alloc] initWithNibName:@"VOCWordTestViewController" bundle:nil];
        testViewController.currentList = selectedList;
        testViewController.testLanguage = selectedLang;
        testViewController.resumePausedTest = NO;
        [self.navigationController pushViewController:testViewController animated:YES];
        [testViewController release];
    }
    
    [actionSheet release];
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger sections = [super numberOfSectionsInTableView:tableView];
    if([VOCWordTest hasPausedTest])
    {
        return sections+1;
    }
    
    return sections;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == SECTION_PAUSED_TEST)
    {
        return 1;
    }
    
    return [super tableView:tableView numberOfRowsInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger section = [indexPath indexAtPosition:0];
   
    if(section == SECTION_PAUSED_TEST)
    {
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        }  
        
        [cell setText:NSLocalizedString(@"UNFINISHED_TEST",nil)];
        
        return cell;
    }
    else
    {
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    
    NSInteger section = [indexPath indexAtPosition:0];

    if(section == SECTION_PAUSED_TEST )
    {
        VOCWordTestViewController *testViewController = [[VOCWordTestViewController alloc] initWithNibName:@"VOCWordTestViewController" bundle:nil];
        testViewController.resumePausedTest = YES;
        [self.navigationController pushViewController:testViewController animated:YES];
        [testViewController release];
    }
    else
    {
        [self beginToTestView];        
    }


}

- (void)dealloc {
    [super dealloc];
}


@end

